<?

	include "rss_reader.php";
	include "../../../xpert_database.php";
	
	function words_format(&$var, $key){
		
		$var = str_replace("_"," ",ltrim(rtrim(ucfirst($var))));
		
	}
	
	$all_feeds = array();

	/*$url = array("http://ocw.mit.edu/OcwWeb/rss/new/mit-newcourses.xml"
	,"http://www.nottingham.ac.uk/toolkits/syndication/"
	,"http://openlearn.open.ac.uk/file.php/1/learningspace.xml"
	,"http://www.nottingham.ac.uk/podcasts/rss.xml");
	
	//echo count($url) . " is the number of feeds <br>";
	
	//while($url_feed = array_pop($url)){
	
		//echo "getting " - $url_feed . "<br>";*/
		
		
	
		$rss_feed = new rss_feed($url_feed);
		
		print_r($argv);
		
		$rss_feed->data = file_get_contents(getcwd() . "/" . $argv[1]);

		$rss_feed->get_feed();
		
		if($rss_feed->failed){

			//echo $url_feed . " feed failed <br>";
		
		}else{
		
			//echo $url_feed . " feed fetched <br>";
		
			if($rss_feed->xml_result==1){
								
				array_push($all_feeds, $rss_feed);
				
				//echo $url_feed . " feed xml worked <br>";
				
			}else{
			
				echo xml_error_string(xml_get_error_code($rss_feed->xml_parser_handle)) . "\n";
			
				array_push($all_feeds, $rss_feed);
						
				//echo $url_feed . " feed xml failed <br>";
			
			}
		
		}
				
	//}
	
	xpert_database_connect($xpert_site);
	
	xpert_database_select($xpert_site);
	
	while($feed_supplier = array_pop($all_feeds)){
	
		while($feed_supplier_items = array_pop($feed_supplier->feed_data)){
								
			if(isset($feed_supplier_items->LINK[0])){
			
				if(!isset($feed_supplier_items->TITLE[0])){
				
					$feed_supplier_items->TITLE[0]="Title not set";
				
				}
				
				if(!isset($feed_supplier_items->{"DC:DESCRIPTION"}[0])){
				
					if(isset($feed_supplier_items->{"DESCRIPTION"}[0])){
				
						$feed_supplier_items->{"DC:DESCRIPTION"}[0]=$feed_supplier_items->{"DESCRIPTION"}[0];
					
					}else{
					
						$feed_supplier_items->{"DC:DESCRIPTION"}[0]= "Description not set";
					
					}
				
				}
				
				if(!isset($feed_supplier_items->{"DC:CONTRIBUTOR"}[0])){
				
					$feed_supplier_items->{"DC:CONTRIBUTOR"}[0]="Contributor not set";
				
				}
				
				if(!isset($feed_supplier_items->{"DC:CREATOR"}[0])){
				
					$feed_supplier_items->{"DC:CREATOR"}[0]="Creator not set";
				
				}
				
				if(!isset($feed_supplier_items->{"DC:FORMAT"}[0])){
				
					$feed_supplier_items->{"DC:FORMAT"}[0]="Format not set";
				
				}
				
				if(!isset($feed_supplier_items->{"DC:FORMAT"}[0])){
				
					$feed_supplier_items->{"DC:FORMAT"}[0]="Format not set";
				
				}
				
				if(!isset($feed_supplier_items->{"DC:LANGUAGE"}[0])){
				
					$feed_supplier_items->{"DC:LANGUAGE"}[0]="Language not set";
				
				}
				
				if(!isset($feed_supplier_items->{"DC:PUBLISHER"}[0])){
				
					$feed_supplier_items->{"DC:PUBLISHER"}[0]="Publisher not set";
				
				}
				
				if(!isset($feed_supplier_items->{"DC:RIGHTS"}[0])){
				
					$feed_supplier_items->{"DC:RIGHTS"}[0]="Rights not set";
				
				}
				
				if(!isset($feed_supplier_items->{"DC:SOURCE"}[0])){
				
					$feed_supplier_items->{"DC:SOURCE"}[0]="Source not set";
				
				}
				
				if(!isset($feed_supplier_items->{"DC:TYPE"}[0])){
				
					$feed_supplier_items->{"DC:TYPE"}[0]="Type not set";
				
				}
				
				if(!isset($feed_supplier_items->{"DC:DATE"}[0])){
				
					if(isset($feed_supplier_items->{"PUBDATE"}[0])){
											
						$date_string = substr($feed_supplier_items->{"PUBDATE"}[0],5);
												
						$values = explode(" ", $date_string);
						
						switch($values[1]){
						
							case "Jan": $values[1] = "01"; break;
							case "Feb": $values[1] = "02"; break;
							case "Mar": $values[1] = "03"; break;
							case "Apr": $values[1] = "04"; break;
							case "May": $values[1] = "05"; break;
							case "Jun": $values[1] = "06"; break;
							case "Jul": $values[1] = "07"; break; 
							case "Aug": $values[1] = "08"; break;
							case "Sep": $values[1] = "09"; break;
							case "Oct": $values[1] = "10"; break;												
							case "Nov": $values[1] = "11"; break;
							case "Dec": $values[1] = "12"; break;
							
						}
						
						if(strlen($values[0])==1){
						
							$values[0] = "0" . $values[0];
						
						}
					
						if(strlen($values[2])==2){
						
							$values[2] = "20" . $values[2];
						
						}
					
						$feed_supplier_items->{"DC:DATE"}[0] = $values[2] . "-" . $values[1] . "-" . $values[0];
											
					}else{
				
						$feed_supplier_items->{"DC:DATE"}[0]="Date not set";
						
					}
				
				}else if(substr($feed_supplier_items->{"DC:DATE"}[0],0,3)!="200"||substr($feed_supplier_items->{"DC:DATE"}[0],0,3)!="201"){
													
					$parts = explode("-",$feed_supplier_items->{"DC:DATE"}[0]);
					
					if(strlen($parts[1])==1){
					
						$parts[1] = "0" . $parts[1];
					
					}
					
					if(strlen($parts[0])==1){
					
						$parts[0] = "0" . $parts[0];
					
					}
					
					$new_date = $parts[2] . "-" . $parts[1] . "-" . $parts[0];
					
					$feed_supplier_items->{"DC:DATE"}[0] = $new_date;
				
				}
				
				echo $feed_supplier_items->{"DC:DATE"}[0] . "\n";

				$query = "INSERT INTO xpert_core_scratch (resource_link, resource_title, resource_date, resource_last_harvest, resource_recommends) VALUES ('" . $feed_supplier_items->LINK[0] . "', '" . addslashes($feed_supplier_items->TITLE[0]) . "','" . addslashes($feed_supplier_items->{"DC:DATE"}[0]) . "','" . time() . "','0') ON DUPLICATE KEY UPDATE resource_title = '" . addslashes($feed_supplier_items->TITLE[0]) . "', resource_date = '" . addslashes($feed_supplier_items->{"DC:DATE"}[0]) . "', resource_last_harvest = '" . time() . "'";
				
				echo $query . "\n";
				
				$result_of_insert = xpert_database_insert_query($query,$xpert_site);
				
				$query = "select resource_id from xpert_core_scratch where resource_link = '" . $feed_supplier_items->LINK[0] ."'";

				$response = xpert_database_select_query($query,$xpert_site);
										
				$last_insert = $response->resource_id;		

				xpert_database_delete_query("delete from xpert_core_description where resource_id = " . $last_insert,$xpert_site);
				
				while($description = array_pop($feed_supplier_items->{"DC:DESCRIPTION"})){
				
					$query = "INSERT INTO xpert_core_description(resource_id, resource_description) VALUES ('" . $last_insert . "', '" . addslashes($description) . "')";
															
					xpert_database_insert_query($query,$xpert_site);
					
				}
				
				xpert_database_delete_query("delete from xpert_core_creator where resource_id = " . $last_insert,$xpert_site);
				
				while($creator = array_pop($feed_supplier_items->{"DC:CREATOR"})){
				
					$query = "INSERT INTO xpert_core_creator(resource_id, resource_creator) VALUES ('" . $last_insert . "', '" . addslashes($creator) . "')";
					
					xpert_database_insert_query($query,$xpert_site);
					
				}
				
				xpert_database_delete_query("delete from xpert_core_relation where resource_id = " . $last_insert,$xpert_site);
				
				if(isset($feed_supplier_items->{"DC:RELATION"})){
				
					while($relation = array_pop($feed_supplier_items->{"DC:RELATION"})){
				
						$query = "INSERT INTO xpert_core_relation(resource_id, resource_relation) VALUES ('" . $last_insert . "', '" . addslashes($relation) . "')";
					
						xpert_database_insert_query($query,$xpert_site);
					
					}
					
				}
				
				xpert_database_delete_query("delete from xpert_core_language where resource_id = " . $last_insert,$xpert_site);
				
				while($language = array_pop($feed_supplier_items->{"DC:LANGUAGE"})){
				
					$query = "INSERT INTO xpert_core_language(resource_id, resource_language) VALUES ('" . $last_insert . "', '" . addslashes($language) . "')";
					
					xpert_database_insert_query($query,$xpert_site);
					
				}
				
				xpert_database_delete_query("delete from xpert_core_subject where resource_id = " . $last_insert,$xpert_site);
				
				if(isset($feed_supplier_items->{"DC:SUBJECT"})){
				
					while($subject = array_pop($feed_supplier_items->{"DC:SUBJECT"})){
				
						$query = "INSERT INTO xpert_core_subject(resource_id, resource_subject) VALUES ('" . $last_insert . "', '" . addslashes($subject) . "')";
					
						xpert_database_insert_query($query,$xpert_site);
					
					}
					
				}
				
				xpert_database_delete_query("delete from xpert_core_publisher where resource_id = " . $last_insert,$xpert_site);
				
				while($publisher = array_pop($feed_supplier_items->{"DC:PUBLISHER"})){
				
					$query = "INSERT INTO xpert_core_publisher(resource_id, resource_publisher) VALUES ('" . $last_insert . "', '" . addslashes($publisher) . "')";
					
					xpert_database_insert_query($query,$xpert_site);
					
				}
				
				xpert_database_delete_query("delete from xpert_core_rights where resource_id = " . $last_insert,$xpert_site);
				
				while($rights = array_pop($feed_supplier_items->{"DC:RIGHTS"})){
				
					$query = "INSERT INTO xpert_core_rights(resource_id, resource_rights) VALUES ('" . $last_insert . "', '" . addslashes($rights) . "')";
					
					xpert_database_insert_query($query,$xpert_site);
					
				}	
				
				xpert_database_delete_query("delete from xpert_core_contributor where resource_id = " . $last_insert,$xpert_site);
				
				while($contributor = array_pop($feed_supplier_items->{"DC:CONTRIBUTOR"})){
				
					$query = "INSERT INTO xpert_core_contributor(resource_id, resource_contributor) VALUES ('" . $last_insert . "', '" . addslashes($contributor) . "')";
					
					xpert_database_insert_query($query,$xpert_site);
					
				}	
				
				xpert_database_delete_query("delete from xpert_core_format where resource_id = " . $last_insert,$xpert_site);
				
				while($format = array_pop($feed_supplier_items->{"DC:FORMAT"})){
				
					$query = "INSERT INTO xpert_core_format(resource_id, resource_format) VALUES ('" . $last_insert . "', '" . addslashes($format) . "')";
					
					xpert_database_insert_query($query,$xpert_site);
					
				}
				
				xpert_database_delete_query("delete from xpert_core_source where resource_id = " . $last_insert,$xpert_site);
				
				while($source = array_pop($feed_supplier_items->{"DC:SOURCE"})){
				
					$query = "INSERT INTO xpert_core_source(resource_id, resource_source) VALUES ('" . $last_insert . "', '" . addslashes($source) . "')";
					
					xpert_database_insert_query($query,$xpert_site);
					
				}
				
				xpert_database_delete_query("delete from xpert_core_type where resource_id = " . $last_insert,$xpert_site);
				
				while($type = array_pop($feed_supplier_items->{"DC:TYPE"})){
				
					$query = "INSERT INTO xpert_core_type(resource_id, resource_type) VALUES ('" . $last_insert . "', '" . addslashes($type) . "')";
					
					xpert_database_insert_query($query,$xpert_site);
					
				}	
			
			}			
	
		}
	
	}

?>